package querysrc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import com.subway.babynote.syncdata.ClientDataRecord;


public class query_addfeedback extends query {
	
	public query_addfeedback(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}	
	public int addfeedback(List<ClientDataRecord.feedbackRecord> records, int user_id)
	{		
		querydb qdb = null;
		Connection con = null;
		try
		{			
			boolean bret = true;
			con = this.init();		
			int iret = -1;
			int count = 0;
			if(bret)
			{
				con.setAutoCommit(false);
				qdb = new querydb(con,sqlscripts.sql_insert_feedbacks);			
				bret = qdb.prePS();
			}
			if(bret)
			{
				Iterator<ClientDataRecord.feedbackRecord> it = records.iterator();
				while(it.hasNext())
				{
					ClientDataRecord.feedbackRecord f = it.next();					
					this.setPSparam(qdb.ps, f, user_id);
					iret = qdb.execPSInsert();
					count++;					
				}
				con.commit();				
			}
			
			return count;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		finally
		{
			this.release_querydb(qdb);
			this.release_connection(con);
			this.finalize();
		}
	}
	
	private int setPSparam(PreparedStatement ps, ClientDataRecord.feedbackRecord f, int user_id)
	{
		//(food_id , user_id , r_date , r_time , quantity ,ts )
		try
		{
			ps.setInt(1, user_id);
			ps.setDate(2, f.r_time);
			ps.setString(3, f.content);			
			return 0;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return -1;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return -1;
		}
	}
}
